package leecode

import (
	"sort"
)

func frequencySort(s string) string {
	hashMap := make(map[byte]int)
	arr := []byte(s)
	for i := 0; i < len(arr); i++ {
		hashMap[arr[i]]++
	}
	tempT := make([][]int, 0)
	for str, count := range hashMap {
		temp := make([]int, 0)
		temp = append(temp, count)
		temp = append(temp, int(str))
		tempT = append(tempT, temp)
	}
	//对二维数组进行排序
	sort.Slice(tempT, func(i, j int) bool {
		return tempT[i][0] > tempT[j][0] // 注意这里是大于号，表示按照频率降序排序
	})
	var res []byte
	for i := 0; i < len(tempT); i++ {
		for count := 0; count < tempT[i][0]; count++ {
			res = append(res, byte(tempT[i][1]))
		}
	}
	return string(res)
}
